package com.gx.smart.lib.http.api;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureProviderGrpc;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureQueryDto;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureQueryResponse;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author xiaosy
 * @create 2020/8/10
 * @Describe
 **/
public class ConstructionAppOrganizationalStructureProviderService {
    private String host = BuildConfig.JOYLIFE_GRPC_SERIVER_URL;
    private String port = "46604";

    private ConstructionAppOrganizationalStructureProviderService() {

    }

    public static ConstructionAppOrganizationalStructureProviderService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private ConstructionAppOrganizationalStructureProviderGrpc.ConstructionAppOrganizationalStructureProviderBlockingStub getStub(ManagedChannel channel) {
        return ConstructionAppOrganizationalStructureProviderGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * ws_grpc_com.ComQuery comQuery = 2; //公共查询体
     * ConstructionAppOrganizationalStructureDto dto = 3;
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ConstructionAppOrganizationalStructureQueryResponse> queryOrganizationalStructure(
            final ComQuery comQuery,
            final String parentId,
            final int level,
            CallBack callBack) {
        return new GrpcAsyncTask<String, Void, ConstructionAppOrganizationalStructureQueryResponse>(callBack) {
            @Override
            protected ConstructionAppOrganizationalStructureQueryResponse doRequestData(ManagedChannel channel) {
                ConstructionAppOrganizationalStructureQueryDto message = ConstructionAppOrganizationalStructureQueryDto.newBuilder()
                        .setComQuery(comQuery)
                        .setDto(ConstructionAppOrganizationalStructureDto.newBuilder()
                                .setParentId(parentId)
                                .setLevel(level)
                                .build())
                        .build();
                Logger.d(message);
                ConstructionAppOrganizationalStructureQueryResponse response = null;
                try {
                    response = getStub(channel).queryOrganizationalStructure(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryOrganizationalStructure");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final ConstructionAppOrganizationalStructureProviderService INSTANCE = new ConstructionAppOrganizationalStructureProviderService();
    }
}
